<?php

/**
 * Framework_Presenter_SmartyExcel
 *
 * @author Application Support Group
 * @package BAMF
 * @filesource
 */


/**
 * Framework_Presenter_SmartyExcel
 *
 * Use this presenter to generate html-based Excel files. The template
 * files for this presenter can live in your normal Smarty templates
 * directory structure. Append "_excel" to the base filename there to use
 * it. You can also assign a special "_filename" variable to this
 * presenter to specify the filename to be used when the Excel file is
 * generated.
 * @author Application Support Group
 * @package BAMF
 */
class Framework_Presenter_SmartyExcel extends Framework_Presenter_Smarty
{
	private $_filename = "output.xls";
	
    /**
     * __construct
     *
	 * @author Application Support Group
     * @access public
     * @param mixed $module Instance of Framework_Module
     * @return void
     */
    public function __construct(Framework_Module $module)
    {
        parent::__construct($module);
        
        $this->pageTemplateFile = SMARTY_DEFAULT_EXCEL_FILE ;
        if ( !empty( $module->pageTemplateFile )) {
        	$this->pageTemplateFile = $module->pageTemplateFile ;
        } else {
        	if ( isset( $this->config['default_excel_tpl'][$this->module->moduleName] ) && !empty( $this->config['default_excel_tpl'][$this->module->moduleName] )) {
        		$this->pageTemplateFile = $this->config['default_excel_tpl'][$this->module->moduleName] ;
        	}
        }
        
    }

    /**
     * display
     *
     *
	 * @author Application Support Group
     * @return void
     */
    public function display()
    {

		$modulePath = get_class( $this->module ) ;		
		$modulePath = str_replace( '_', '/', $modulePath ); 
		$requestedTemplate = SMARTY_TEMPLATE_PATH.$modulePath.'_excel.tpl' ;
		$modulePath = str_replace( 'Module/', '', $modulePath ) ;
		$tplFile = '' ;

		$this->template->assign( 'modulePath', $modulePath ) ;
		$this->template->assign( 'moduleClass', get_class( $this->module )) ;
		$this->template->assign( 'moduleUuid', $this->module->uuid ) ;

		if (file_exists($requestedTemplate)) {
			$this->template->assign('requestedTemplate', $requestedTemplate) ;
		} else {
			$this->template->assign('originalRequest', $requestedTemplate) ;
			$this->template->assign('requestedTemplate', '') ;			
		}

		foreach ($this->module->getData() as $var => $val) {
			if (!in_array( $var, $this->reservedVariableNames )) {
				if ($var{0} == "_") {
					$this->$var = $val ;
				} else {
					$this->template->assign($var,$val);
				}
				
			}
		}


		header( "Cache-Control: must-revalidate, post-check=0,pre-check=0" );
		header( "Pragma: public" );
		header( "Content-Type: application/force-download" );
       	header( "Content-Disposition: inline; filename=".$this->_filename );

		$this->template->display($this->pageTemplateFile);

		return true ;


    }

    public function __destruct()
    {
        parent::__destruct();
    }
}

?>
